import { FormulaGateway, Formula, FormulaInterfaceWithDerive, PageData, DisableRes, PutRes, putFormulaDto, disableFormulaDto } from "qqlx-core";

import { ViewBase, ViewBaseAction } from "@view/view-base";
import { FormulaSchema } from "@/pond/schema-asset";

export class ViewFormula extends ViewBase<Formula> implements ViewBaseAction<Formula>, FormulaInterfaceWithDerive<{}> {
  private static instance: ViewFormula;
  static getInstance(options?: { isSingleton?: boolean }) {
    if (options?.isSingleton === false) {
      return new ViewFormula();
    }

    if (!this.instance) this.instance = new ViewFormula();
    return this.instance;
  }

  private constructor() {
    super();
    this.initial();
    this.insertDefaultCondition();
  }

  getSchema(): Formula {
    return new FormulaSchema();
  }

  async get() {
    this.pending();
    const { list, total } = await this.$request.getByPut<null, PageData<Formula>>(FormulaGateway.path);
    this.data.entityListTotal = total;
    return { list, total };
  }

  async put() {
    this.pending();
    const dto = { schema: this.data.schema };
    return this.$request.put<putFormulaDto, PutRes<Formula>>(FormulaGateway.path, dto);
  }

  async disable(dto: disableFormulaDto) {
    this.pending();
    return this.$request.delete<disableFormulaDto, DisableRes>(FormulaGateway.path, dto);
  }
}
